(function ($) {
    $.fn.banner = function (options) {
        var defaults = {
            timer: 4,
            speed: 400,
            moveSpeed: 300,
            width: $(this).width()
        };

        var options = $.extend(defaults, options);

        var $imgWrapper = $(this);
        var $imgBox = $("ul", $imgWrapper);

        var imgCounts = $("img", $imgWrapper).length;

        $imgBox.css({"width": imgCounts * 100 + '%'});
        $("li", $imgWrapper).css({"width": 100 / imgCounts + '%'});

        if (imgCounts <= 1) {
            return false;
        }

        var initImgCounts = function () {
            var _html = "<div class='pic-cursor-bar'>";
            for (var i = 0; i < imgCounts; i++) {
                _html += "<a href='javascript:void(0)'></a>";
            }
            _html += "</div>";
            $(".pic-bottom").html(_html);
            $(".pic-cursor-bar a:eq(0)").addClass("pic-current");
        };

        initImgCounts();

        var current = 0;

        function next(speed) {
            $imgBox.animate({'margin-left': '-100%'}, speed, function () {
                $imgBox.find('li').eq(0).appendTo($imgBox);
                $imgBox.css({'margin-left': 0});
            });
            current++;
            $(".pic-cursor-bar a").removeClass().eq(current % imgCounts).addClass('pic-current');
        }

        function prev(speed) {
            $imgBox.find('li:last').prependTo($imgBox);
            $imgBox.css({'margin-left': '-100%'});
            $imgBox.animate({'margin-left': 0}, speed);
            current--;
            $(".pic-cursor-bar a").removeClass().eq(current % imgCounts).addClass('pic-current');
        }

        var timer = null;

        function play() {
            timer = setInterval(function () {
                next(options.speed);
            }, options.timer * 1000);
        }

        play();

        $(".arr-left").bind("click", function () {
            clearInterval(timer);
            prev(options.speed);
            play();
        });

        $(".arr-right").bind("click", function () {
            clearInterval(timer);
            next(options.speed);
            play();
        });
    }
})(jQuery);